package com.chat.chatting.service.impl;

import com.chat.chatting.dao.UserDao;
import com.chat.chatting.dto.DataJson;
import com.chat.chatting.dto.UserAddFormDto;
import com.chat.chatting.entity.UserEntity;
import com.chat.chatting.service.FriendService;
import com.chat.chatting.service.UserService;
import com.chat.chatting.util.ImageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2021-12-24 21:39:09
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private FriendService friendService;

    /**
     * 通过用户ID查询单条数据
     *
     * @param userID 用户ID
     * @return 实例对象
     */
    @Override
    public UserEntity queryByUserID(Integer userID) {
        return userDao.queryByUserID(userID);
    }

    @Override
    public Integer create(UserAddFormDto addFormDto) {
        UserEntity entity = new UserEntity();
        BeanUtils.copyProperties(addFormDto, entity);
//        entity.setPkID(UUIDUtil.generateUUID());
        userDao.create(entity);
        return entity.getUserID();
    }

    @Override
    public DataJson login(Integer userID, String password) {
        DataJson result = new DataJson();
        if (userID == null || userID.equals("")) {
            result.setCode(DataJson.DEFEAT);
            result.setMsg("请输入用户ID");
        } else {
            UserEntity entity = userDao.queryByUserID(userID);
            if (entity == null) {
                result.setCode(DataJson.DEFEAT);
                result.setMsg("该用户ID不存在");
                return result;
            } else {
                if (entity.getPassword().equals(password)) {
                    result.setCode(DataJson.SUCCESS);
                    result.setMsg("登录成功");
                } else {
                    result.setCode(DataJson.DEFEAT);
                    result.setMsg("密码错误");
                }
            }
        }
        return result;
    }

    @Override
    public DataJson updateNickName(Integer userID, String nickName) {
        DataJson result = new DataJson();
        if (nickName == null || nickName.equals("")) {
            result.setCode(DataJson.DEFEAT);
            result.setMsg("昵称不可为空");
        } else {
            UserEntity entity = queryByUserID(userID);
            if (entity == null) {
                result.setCode(DataJson.DEFEAT);
                result.setMsg("用户不存在");
            } else {
                entity.setNickName(nickName);
                userDao.updateNickName(userID, nickName);
                result.setCode(DataJson.SUCCESS);
                result.setMsg("修改成功");
            }
        }
        return result;
    }

    @Override
    public DataJson uploadImage(MultipartFile multipartFile, Integer userID) {
        //调用上传工具类对图片进行保存
        String imageName = ImageUtils.upload(multipartFile);
        DataJson dataJson = new DataJson();
        if (imageName != null) {
            //修改数据库中的头像路径
            userDao.updatePhoto(imageName, userID);
            //修改好友表中的头像
            friendService.updatePhotos(userID, imageName);
            dataJson.setCode(DataJson.SUCCESS);
            dataJson.setMsg("上传成功");
            HashMap<String, String> map = new HashMap<>();
            map.put("src", imageName);
            dataJson.setData(map);
        } else {
            dataJson.setCode(DataJson.DEFEAT);
            dataJson.setMsg("上传失败");
        }
        return dataJson;
    }

    @Override
    public String getImage(Integer userID) {
//        return ImageUtils.SERVER_PATH + userDao.getImage(userID);
        return userDao.getImage(userID);
    }

}